package com.xlh.service.problem.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.problem.ProblemMapper;
import com.xlh.dao.problem.ProblemMapperExt;
import com.xlh.dto.problem.ProblemDTO;
import com.xlh.dto.problem.UserProblemAndAnswerDTO;
import com.xlh.param.problem.ProblemParam;
import com.xlh.param.problem.ProblemPostParam;
import com.xlh.param.problem.ReplyPostParam;
import com.xlh.pojo.problem.Problem;
import com.xlh.pojo.problem.ProblemExample;
import com.xlh.service.problem.ProblemService;
import com.xlh.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    private ProblemMapper mapper;
    @Autowired
    private ProblemMapperExt mapperExt;

    @Override
    public List<ProblemDTO> listProblemPages(ProblemParam problemParam) {
        return mapperExt.listProblemPages(problemParam);
    }

    @Override
    public ProblemDTO problemDetailById(Long id) {
        return mapperExt.problemDetailById(id);
    }

    @Override
    public List<ProblemDTO> listMyProblemReplyPages(ProblemParam problemParam) {
        return mapperExt.listMyProblemReplyPages(problemParam);
    }

    @Override
    public void askQuestions(ProblemPostParam problemPostParam) {
        Problem problem = BeanUtil.transform(Problem.class, problemPostParam);
        mapper.insertSelective(problem);
    }

    @Override
    public void batchDeletionQuestion(List<Integer> ids) {
        mapperExt.batchDeletionQuestion(ids);
    }

    @Override
    public void changeNumberAnswers(ReplyPostParam replyPostParam) {
        mapperExt.changeNumberAnswers(replyPostParam);
    }

    @Override
    public List<UserProblemAndAnswerDTO> findProblemAndAnswerInfoByUser(Long courseId, List<Long> userIds) {

        // 查询提问问题总数
        List<UserProblemAndAnswerDTO> userProblemCounts = mapperExt.findUserProblemCount(courseId, userIds);
        // 查询回答问题总数
        List<UserProblemAndAnswerDTO> userAnswerCounts =  mapperExt.findUserAnswerCount(courseId, userIds);
        // 合并查询出来的结果集
        List<UserProblemAndAnswerDTO> recordData = mergeProblemAndAnswer(userProblemCounts, userAnswerCounts);
        // 完整的user结果集再次合并
        outer:
        for (Long userId : userIds) {
            for (UserProblemAndAnswerDTO record : recordData) {
                Long recordUserId = record.getUserId();
                if(userId.equals(recordUserId)){
                    continue outer;
                }
            }
            UserProblemAndAnswerDTO recordTemp = new UserProblemAndAnswerDTO();
            recordTemp.setUserId(userId);
            recordTemp.setAnswerCount(0L);
            recordTemp.setProblemCount(0L);
            recordTemp.setCourseId(courseId);
            // 未获取到的记录存入结果集中
            recordData.add(recordTemp);
        }
        return recordData;
    }

    /**
     * 合并问题和答案总结果集
     * @param userProblemCounts
     * @param userAnswerCounts
     * @return
     */
    private List<UserProblemAndAnswerDTO> mergeProblemAndAnswer(List<UserProblemAndAnswerDTO> userProblemCounts, List<UserProblemAndAnswerDTO> userAnswerCounts) {

        if(userProblemCounts == null){
            userProblemCounts = Lists.newArrayList();
        }
        CopyOnWriteArrayList<UserProblemAndAnswerDTO> userProblemAndAnswerDTOS = Lists.newCopyOnWriteArrayList(userAnswerCounts);
        for (UserProblemAndAnswerDTO userProblemCount : userProblemCounts) {
            for (UserProblemAndAnswerDTO userAnswerDTO : userProblemAndAnswerDTOS) {
                if(userProblemCount.getUserId().equals(userAnswerDTO.getUserId())){
                    userProblemCount.setAnswerCount(userAnswerDTO.getAnswerCount());
                    userProblemAndAnswerDTOS.remove(userAnswerDTO);
                }
            }
        }
        userProblemCounts.addAll(userProblemAndAnswerDTOS);
        return userProblemCounts;
    }
}
